export const defaultOperator: Operator = 'eq'

export const operatorValues = [
  'eq',
  'gt',
  'lt',
  'ge',
  'le',
  'like',
  'in',
  'between',
]
export type Operator =
  | 'eq'
  | 'gt'
  | 'lt'
  | 'ge'
  | 'le'
  | 'like'
  | 'in'
  | 'between' // 与handlers/models.rs中的Operator对应

export enum FieldType {
  String = 'string',
  Boolean = 'bool',
  Number = 'number',
  DateTime = 'datetime',
}

/**
 * 基础字段类型
 */
export interface Field {
  name: string
  prop: string
  label: string
  type: 'input' | 'select' | 'date' | 'daterange' | 'custom'
  field_type: string
}

/**
 * 选项字段类型，扩展基础字段
 */
export interface OptionField extends Field {
  alwaysSelected: boolean
  selected?: boolean
}

export const fieldTypeOperatorMapping: Record<FieldType, Operator[]> = {
  [FieldType.String]: ['eq', 'like'],
  [FieldType.Boolean]: ['eq'],
  [FieldType.Number]: ['eq', 'gt', 'ge', 'lt', 'le'],
  [FieldType.DateTime]: ['between'],
}

export interface WarningConfigItem extends Field {
  operator: Operator
  value: any
}

export interface WarningConfig {
  id: string // 数据的唯一性标识
  tableName: string
  conditionFields: WarningConfigItem[] // 查询条件的字段
  dataFields: string[] // 显示的字段名称
  scanInterval: number // 扫描间隔，单位秒
  offsetIndexField: WarningConfigItem
}

export interface WarningExecutionLog {
  id: string
  configId: string
  startTime: string
  macthedCount: number
}

/**
 * 搜索字段类型，扩展基础字段
 */
export interface SearchField extends OptionField {
  placeholder?: string
  clearable?: boolean
  colSpan?: number
  options?: Array<{ label: string; value: string | number }>
  props?: Record<string, any>
  operator?: Operator
  getValue?: (value: any) => Promise<any>
  validate?: (value: any) => Promise<boolean>
}

export interface TableField extends OptionField {
  width?: string | number
  align?: 'left' | 'center' | 'right'
  customRender?: (params: { row: any }) => any
}

export interface Table {
  name: string
  fields: Array<TableField>
}

export interface SearchParam {
  name: string
  value: any
  operator: Operator
  field_type: string
}
